<template>
    <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
        <el-form class="tl-form" ref="confirmform" :model="model" :reules="rules" label-width="120px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="确认日期" prop="confirmtime" :rules="rules.validateRequire">
                        <el-date-picker v-model="model.confirmtime" type="date" placeholder="确认日期" value-format="yyyy-MM-dd"></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="确认结果" prop="confirmresult" :rules="rules.validateRequire">
                        <el-input v-model="model.confirmresult"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="120px" :disabled="disableEditSubmit || disableSave">
            <el-row>
                <el-col :span="24">
                    <el-divider content-position="center"><span>【以下信息为只读信息】</span></el-divider>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="项目" prop="projectid" :required="true" :rules="rules.validateRequire">
                        <el-input v-model="model.dispprojectid" :disabled="true" class="tl-ref-selector">
                            <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmProjectRefDialog" v-if="!disableEditSubmit && isAdd"></el-button>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="相关方" prop="stakeholderid" :required="true" :rules="rules.validateRequire">
                        <el-input v-model="model.dispstakeholderid" :disabled="true" class="tl-ref-selector">
                            <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmStakeholerRefDialog" v-if="!(disableEditSubmit || disableSave)"></el-button>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="活动名称" prop="activityname" :rules="rules.validateRequire">
                        <el-input v-model="model.activityname" type="textarea" :rows="2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="计划人" prop="plannername" :required="true" :rules="rules.validateRequire">
                        <el-input v-model="model.plannername" :disabled="true" class="tl-ref-selector">
                            <el-button slot="append" type="primary" icon="el-icon-search" @click="openPmTeamPlannerRefDialog" v-if="!(disableEditSubmit || disableSave)"></el-button>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="计划人工号" prop="plannercode">
                        <el-input v-model="model.plannercode"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="计划日期" prop="plantime">
                        <el-date-picker v-model="model.plantime" type="date" placeholder="计划日期" value-format="yyyy-MM-dd"></el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="计划内容" prop="plancontent">
                        <el-input v-model="model.plancontent" type="textarea" :rows="2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="执行跟踪人" prop="executiontrackername">
                        <el-input v-model="model.executiontrackername" :disabled="true" class="tl-ref-selector">
                            <el-button slot="append" type="primary" icon="el-icon-search" @click="openPmTeamExecutiontrackerRefDialog" v-if="!(disableEditSubmit || disableSave)"></el-button>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="执行跟踪人工号" prop="executiontrackercode">
                        <el-input v-model="model.executiontrackercode"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="执行日期" prop="executiontime" :required="true">
                        <el-date-picker v-model="model.executiontime" type="date" placeholder="执行日期" value-format="yyyy-MM-dd"></el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="执行情况" prop="implementation" :required="true">
                        <el-input v-model="model.implementation" type="textarea" :rows="2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="确认人" prop="confirmbyname">
                        <el-input v-model="model.confirmbyname" :disabled="true" class="tl-ref-selector">
                            <el-button slot="append" type="primary" icon="el-icon-search" @click="openPmTeamConfirmerRefDialog" v-if="!(disableEditSubmit || disableSave)"></el-button>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="确认人工号" prop="confirmbycode">
                        <el-input v-model="model.confirmbycode"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="备注" prop="description">
                        <el-input v-model="model.description" type="textarea" :rows="2"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-collapse>
                <el-collapse-item title="【单据】相关信息">
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="申请公司" prop="orgname">
                                <el-input v-model="model.orgname" :readonly="true" class="is-disabled"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="申请部门" prop="deptname" :required="true" :rules="rules.validateRequire">
                                <tl-ref-dept v-model="model.deptcode" :initDisplayData.sync="model.deptname" dataType="code" nameType="fullname" :disabled="true" :singleSelect="true" @confirm="onSelectDept" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="申请人工号" prop="usercode">
                                <el-input v-model="model.usercode" :readonly="true" class="is-disabled"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="申请人姓名" prop="username" :required="true" :rules="rules.validateRequire">
                                <tl-ref-user v-model="model.usercode" :initDisplayData.sync="model.username" dataType="code" :disabled="true" :singleSelect="true"></tl-ref-user>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="单据日期" prop="docdate">
                                <el-date-picker v-model="model.docdate" type="date" placeholder="单据日期" value-format="yyyy-MM-dd"></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="提交时间" prop="submitdate">
                                <el-date-picker v-model="model.submitdate" type="datetime" placeholder="提交时间" value-format="yyyy-MM-dd HH:mm:ss" disabled></el-date-picker>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="审批流程" prop="flowid">
                                <el-input v-model="model.dispflowid" :readonly="true" class="is-disabled"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="状态" prop="state">
                                <el-input v-model="model.dispstate" disabled></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-collapse-item>
            </el-collapse>
        </el-form>
        <tl-approval-history ref="history" sourceType="PmStakeholderEngagement" :sourceId="model.id" />
        <div slot="footer" class="dialog-footer">
            <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit" v-if="uiOperateState<=2">取消</el-button>
            <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disableSave">
                <svg-icon icon-class="save" />保存</el-button>
            <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval">
                <svg-icon icon-class="send" />提交审批</el-button>
            <tl-attachment sourceType="PmStakeholderEngagement" :sourceId="model.id" :disabled="disableAttachment" :editable="stateEdit"></tl-attachment>
            <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
                <svg-icon icon-class="list" />审批历史</el-button>
            <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableEditSubmit" :disabled="disableDelApproval">
                <svg-icon icon-class="undo" />取消审批</el-button>
        </div>
        <tlpm-project-ref ref="pmProjectRef" @confirm="getpmProjectRefData" :isSingleSelect="true"></tlpm-project-ref>
        <tlpm-stakeholer-ref ref="pmStakeholerRef" @confirm="getpmStakeholerRefData" :isSingleSelect="false" :externalQueryParam="{ qry_projectid: model.projectid }"></tlpm-stakeholer-ref>
        <tlpm-team-ref ref="pmTeamPlannerRef" @confirm="getPmTeamPlannerRefData" :isSingleSelect="true" :externalQueryParam="{ qry_projectid: model.projectid }"></tlpm-team-ref>
        <tlpm-team-ref ref="pmTeamExecutiontrackerRef" @confirm="getPmTeamExecutiontrackerRefData" :isSingleSelect="true" :externalQueryParam="{ qry_projectid: model.projectid }"></tlpm-team-ref>
        <tlpm-team-ref ref="pmTeamConfirmerRef" @confirm="getPmTeamConfirmerRefData" :isSingleSelect="true" :externalQueryParam="{ qry_projectid: model.projectid }"></tlpm-team-ref>
    </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import tlpmProjectRef from '@/views/ProjectManage/pmProject/refList'
import tlpmStakeholerRef from '@/views/ProjectManage/pmStakeholder/refList'
import tlpmTeamRef from '@/views/ProjectManage/pmTeam/refList'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
    mixins: [mixin_approval_edit],
    components: { tlpmProjectRef, tlpmStakeholerRef, tlpmTeamRef },
    data() {
        return {
            moduleTitle: '相关方参与管理',
            urlPrefix: '/pm/stakeholderEngagement/',
            url: {
                // 额外的url
                queryOrgByDeptId: '/pm/resourceArchives/queryOrgByDeptId'
            }
        }
    },
    methods: {
        assignDefaultValue() {
            if (this.isAdd) {
                if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
                    this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
                }
                this.$set(this.model, 'orgcode', this.org.code)
                this.$set(this.model, 'orgname', this.org.name)
                this.$set(this.model, 'deptcode', this.dept.code)
                this.$set(this.model, 'deptname', this.dept.fullname)
                this.$set(this.model, 'usercode', this.user.code)
                this.$set(this.model, 'username', this.user.name)
            }
        },
        loaded() {
            // 页面加载完毕执行
            if (!this.isAdd && this.model.docno) {
                this.title = this.title + `（单据编号：${this.model.docno}）`
            }
        },
        /** 打开项目参照 */
        openpmProjectRefDialog() {
            this.$refs.pmProjectRef.visible = true
        },
        /** 获取项目参数数据 */
        getpmProjectRefData(selectionRow, selectKey, selectionValue) {
            this.$set(this.model, 'projectid', selectionRow.id)
            this.$set(this.model, 'dispprojectid', selectionRow.name)
        },
        checkSelectProject() {
            if (!this.model.projectid) {
                this.$tap.alert('请先选择项目')
                return false
            }
            return true
        },
        /** 打开相关方参照 */
        openpmStakeholerRefDialog() {
            if (this.checkSelectProject()) {
                this.$refs.pmStakeholerRef.visible = true
            }
        },
        /** 获取相关方参数数据 */
        getpmStakeholerRefData(selectionRows, selectKeys, selectionValues) {
            this.$set(this.model, 'stakeholderid', selectKeys)
            this.$set(this.model, 'dispstakeholderid', selectionValues)
        },
        /** 打开计划人参照 */
        openPmTeamPlannerRefDialog() {
            if (this.checkSelectProject()) {
                this.$refs.pmTeamPlannerRef.visible = true
            }
        },
        /** 获取计划人参数数据 */
        getPmTeamPlannerRefData(selectionRow, selectKey, selectionValue) {
            this.$set(this.model, 'plannername', selectionRow.username)
            this.$set(this.model, 'plannercode', selectionRow.usercode)
        },
        /** 打开执行跟踪人参照 */
        openPmTeamExecutiontrackerRefDialog() {
            if (this.checkSelectProject()) {
                this.$refs.pmTeamExecutiontrackerRef.visible = true
            }
        },
        /** 获取执行跟踪人参数数据 */
        getPmTeamExecutiontrackerRefData(selectionRow, selectKey, selectionValue) {
            this.$set(this.model, 'executiontrackername', selectionRow.username)
            this.$set(this.model, 'executiontrackercode', selectionRow.usercode)
        },
        /** 打开确认人参照 */
        openPmTeamConfirmerRefDialog() {
            if (this.checkSelectProject()) {
                this.$refs.pmTeamConfirmerRef.visible = true
            }
        },
        /** 获取确认人参数数据 */
        getPmTeamConfirmerRefData(selectionRow, selectKey, selectionValue) {
            this.$set(this.model, 'confirmbyname', selectionRow.username)
            this.$set(this.model, 'confirmbycode', selectionRow.usercode)
        },
        onSelectDept(selectedDepts) {
            const selectedDept = selectedDepts[0]
            const param = {
                deptId: selectedDept.id
            }
            getAction(this.url.queryOrgByDeptId, param).then(res => {
                if (res.success) {
                    const { code: orgCode, name: orgName } = res.result
                    this.$set(this.model, 'orgcode', orgCode)
                    this.$set(this.model, 'orgname', orgName)
                } else {
                    this.$tap.alert('已选部门关联公司信息错误，请联系系统管理员')
                }
            })
        },
        saveConfirmInfo(resolve, reject) {
            const { id, confirmtime, confirmresult } = this.model
            if (!confirmtime) {
                this.$tap.alert('请填写确认日期')
                reject()
            }
            if (!confirmresult) {
                this.$tap.alert('请填写确认结果')
                reject()
            }
            const param = {
                id,
                confirmtime,
                confirmresult
            }
            putAction(this.url.edit, param)
                .then(res => {
                    if (res.success) {
                        resolve()
                    } else {
                        this.$tap.alert(res.message, 'warning')
                        reject()
                    }
                })
                .catch(err => {
                    this.$tap.alert('保存确认信息失败，请联系系统管理员', 'warning')
                    reject()
                })
        }
    }
}
</script>
